//
//  UserInfoViewController.m
//  家校互通
//
//  Created by 姬鑫宇 on 15/5/11.
//  Copyright (c) 2015年 姬鑫宇. All rights reserved.
//

#import "UserInfoViewController.h"
#import "MainViewController.h"
#import "ViewController.h"
#import "CoModel.h"
#import "APService.h"
@interface UserInfoViewController ()

@end

@implementation UserInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _nextBtn.layer.masksToBounds=YES;
    _nextBtn.layer.cornerRadius=_nextBtn.frame.size.height/2;
    
  
    NSString *registrationId = [APService registrationID];
    NSString *idfv = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
    NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];
    
    if (_isFromLoginView) {
        _infoLabel.text = [DSFUserInfo shareUserInstance].clazz_user.nickname;
        return;
    }
    // Do any additional setup after loading the view.
    [DSFUserInfo userLogin:^(BOOL result, NSError *error) {
        if (result) {
             _infoLabel.text = [DSFUserInfo shareUserInstance].clazz_user.nickname;
            if ([DSFUserInfo shareUserInstance].isTeachLogin)
            {
                [_backBtn setTitle:@"我是老师" forState:UIControlStateNormal];
            }
            else
            {
                 [_backBtn setTitle:@"我是家长" forState:UIControlStateNormal];
            }
            [DSFUserInfo getClazzUsers:^(BOOL result, NSArray *classUserArr, NSError *error) {
                
            } withToken:[DSFUserInfo shareUserInstance].token andClazzId:[DSFUserInfo shareUserInstance].clazz_user.clazz.id];
        }else{
            
            UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提示" message:@"验证失败，请重新登陆" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
            alertView.tag=100;
            [alertView show];
        }
    } withUsername:[defaults objectForKey:@"username"] andPassword:[defaults objectForKey:@"password"] andUserType:[[defaults objectForKey:@"userType"] intValue] andPushUserId:registrationId andImei:idfv];
   
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (alertView.tag==100) {
        ViewController *vc=[self.storyboard instantiateViewControllerWithIdentifier:@"ViewController"];
        [self.navigationController pushViewController:vc animated:YES];
    }
}
- (void)viewWillAppear:(BOOL)animated{
    [self.navigationController setNavigationBarHidden:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)changeUserType:(id)sender{
    [DSFUserInfo userLogout:^(BOOL result, NSError *error) {
        if (_isFromLoginView) {
            [self.navigationController popViewControllerAnimated:YES];
        }else
        {
            ViewController *vc=[self.storyboard instantiateViewControllerWithIdentifier:@"ViewController"];
            _isFromLoginView=NO;
            [self.navigationController pushViewController:vc animated:YES];
        }
    }];
}

- (IBAction)nextPressed:(id)sender{
    MainViewController *vc = [self.storyboard  instantiateViewControllerWithIdentifier:@"MainViewController"];
    self.navigationItem.backBarButtonItem=[[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil];
    [self.navigationController pushViewController:vc animated:YES];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
